package com.wswy.wzcx.model.license;

import android.os.Parcel;
import android.os.Parcelable.Creator;
import android.support.annotation.Keep;
import com.blankj.utilcode.util.TimeUtils;
import com.google.gson.annotations.SerializedName;

import com.wswy.wzcx.model.car.CarExtendInfo;
import com.wswy.wzcx.model.car.CarTypeData;
import com.wswy.wzcx.model.car.DriverLicense;
import java.io.Serializable;
import java.util.Date;

@Keep
public class UserCarInfo implements android.os.Parcelable {

  @SerializedName("userCarId")
  public int id;


  @SerializedName("cityId")
  public int cityId;

  @SerializedName("carNo")
  public String carNo;

  @SerializedName("carType")
  public String carType;

  @SerializedName("totalFineDeductPoints")
  public int totalScore;

  @SerializedName("totalFineFee")
  public int totalFee;

  @SerializedName("totalFineNum")
  public int trafficCount;

  @SerializedName("frameId")
  public String frameNo;

  @SerializedName("engineId")
  public String engineNo;

  @SerializedName("lastQueryTime")
  public long lastDate;


  @SerializedName("userCarExtend")
  public CarExtendInfo carExtendInfo;

  @SerializedName("userDriverLicense")
  public DriverLicense driverLicense;

  private Date mLastDate;

  //查询失败
  public transient boolean isError = false;

  @SerializedName("_extraError")
  public ExtraError extraError;


  protected UserCarInfo(Parcel in) {
    id = in.readInt();
    cityId = in.readInt();
    carNo = in.readString();
    carType = in.readString();
    totalScore = in.readInt();
    totalFee = in.readInt();
    trafficCount = in.readInt();
    frameNo = in.readString();
    engineNo = in.readString();
    lastDate = in.readLong();
    carExtendInfo = in.readParcelable(CarExtendInfo.class.getClassLoader());
    driverLicense = in.readParcelable(DriverLicense.class.getClassLoader());
    extraError = in.readParcelable(ExtraError.class.getClassLoader());
  }

  public static final Creator<UserCarInfo> CREATOR = new Creator<UserCarInfo>() {
    @Override
    public UserCarInfo createFromParcel(Parcel in) {
      return new UserCarInfo(in);
    }

    @Override
    public UserCarInfo[] newArray(int size) {
      return new UserCarInfo[size];
    }
  };

  public Date getLastDate() {
    if(mLastDate == null && lastDate != 0){
      mLastDate = TimeUtils.millis2Date(lastDate*1000);

      Date nowDate = TimeUtils.getNowDate();
      if (mLastDate.compareTo(nowDate) > 0){
        mLastDate = nowDate;
      }

    }
    if(mLastDate == null){
      mLastDate = TimeUtils.getNowDate();
    }

    return mLastDate;
  }

  public UserCarInfo() {
  }


  @Override
  public String toString() {
    return "UserCarInfo{" +
        "id=" + id +
        ", carType='" + carType + '\'' +
        ", carNo='" + carNo + '\'' +
        ", frameNo='" + frameNo + '\'' +
        ", engineNo='" + engineNo + '\'' +
        ", lastDate='" + lastDate + '\'' +
        '}';
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }

    UserCarInfo that = (UserCarInfo) o;

    if(id != 0){
      if (id != that.id) {
        return false;
      }else {
        return true;
      }
    }

    if (!carType.equals(that.carType)) {
      return false;
    }
    return carNo.equals(that.carNo);
  }

  @Override
  public int hashCode() {
    int result = id;
    result = 31 * result + carType.hashCode();
    result = 31 * result + carNo.hashCode();
    return result;
  }


  @Override
  public int describeContents() {
    return 0;
  }

  @Override
  public void writeToParcel(Parcel dest, int flags) {
    dest.writeInt(id);
    dest.writeInt(cityId);
    dest.writeString(carNo);
    dest.writeString(carType);
    dest.writeInt(totalScore);
    dest.writeInt(totalFee);
    dest.writeInt(trafficCount);
    dest.writeString(frameNo);
    dest.writeString(engineNo);
    dest.writeLong(lastDate);
    dest.writeParcelable(carExtendInfo, flags);
    dest.writeParcelable(driverLicense, flags);
    dest.writeParcelable(extraError, flags);
  }
}
